package com.example.cursor_controller;

import com.example.cursor_service.UserService;
import com.example.cursor_orm.model.User;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.server.ResponseStatusException;

import java.util.List;

@RestController
@RequestMapping("/users")
@Tag(name = "用户管理", description = "用户相关的API接口")
public class UserController {
    private final UserService userService;
    
    @Autowired
    public UserController(UserService userService) {
        this.userService = userService;
    }
    
    @PostMapping
    @Operation(summary = "创建用户", description = "创建新用户")
    @ApiResponses(value = {
        @ApiResponse(responseCode = "200", description = "用户创建成功"),
        @ApiResponse(responseCode = "400", description = "请求参数错误")
    })
    public User createUser(@RequestBody User user) { 
        return userService.createUser(user); 
    }
    
    @GetMapping("/{id}")
    @Operation(summary = "根据ID获取用户", description = "根据用户ID获取用户信息")
    @ApiResponses(value = {
        @ApiResponse(responseCode = "200", description = "获取用户成功"),
        @ApiResponse(responseCode = "404", description = "用户不存在")
    })
    public User getUserById(@Parameter(description = "用户ID") @PathVariable Integer id) { 
        return userService.getUserById(id); 
    }
    
    @PutMapping("/{id}")
    @Operation(summary = "更新用户", description = "根据用户ID更新用户信息")
    @ApiResponses(value = {
        @ApiResponse(responseCode = "200", description = "用户更新成功"),
        @ApiResponse(responseCode = "404", description = "用户不存在")
    })
    public User updateUser(@Parameter(description = "用户ID") @PathVariable Integer id, 
                          @RequestBody User user) { 
        return userService.updateUser(id, user); 
    }
    
    @DeleteMapping("/{id}")
    @Operation(summary = "删除用户", description = "根据用户ID删除用户")
    @ApiResponses(value = {
        @ApiResponse(responseCode = "200", description = "用户删除成功"),
        @ApiResponse(responseCode = "404", description = "用户不存在")
    })
    public boolean deleteUser(@Parameter(description = "用户ID") @PathVariable Integer id) { 
        return userService.deleteUser(id); 
    }
    
    @GetMapping
    @Operation(summary = "获取用户列表", description = "分页获取用户列表")
    @ApiResponses(value = {
        @ApiResponse(responseCode = "200", description = "获取用户列表成功")
    })
    public List<User> getUserList(@Parameter(description = "页码", example = "1") @RequestParam(defaultValue = "1") int page, 
                                @Parameter(description = "每页大小", example = "10") @RequestParam(defaultValue = "10") int size) { 
        return userService.getUserList(page, size); 
    }
    
    @PostMapping("/login")
    @Operation(summary = "用户登录", description = "用户登录验证")
    @ApiResponses(value = {
        @ApiResponse(responseCode = "200", description = "登录成功"),
        @ApiResponse(responseCode = "401", description = "用户名或密码错误")
    })
    public User login(@RequestBody User loginUser) {
        User user = userService.login(loginUser.getUsername(), loginUser.getPasswordHash());
        if (user == null) {
            // 登录失败，抛出401异常，并带上自定义message
            throw new ResponseStatusException(HttpStatus.UNAUTHORIZED, "用户名或密码错误");
        }
        return user;
    }
    
    @PostMapping("/logout")
    @Operation(summary = "用户登出", description = "用户登出")
    @ApiResponses(value = {
        @ApiResponse(responseCode = "200", description = "登出成功")
    })
    public void logout(@Parameter(description = "用户ID") @RequestParam Integer userId) { 
        userService.logout(userId); 
    }
    
    @PostMapping("/password/reset")
    @Operation(summary = "重置密码请求", description = "发送密码重置请求")
    @ApiResponses(value = {
        @ApiResponse(responseCode = "200", description = "重置请求发送成功"),
        @ApiResponse(responseCode = "404", description = "用户不存在")
    })
    public boolean resetPasswordRequest(@Parameter(description = "用户名") @RequestParam String username) { 
        return userService.resetPasswordRequest(username); 
    }
    
    @PutMapping("/password")
    @Operation(summary = "更新密码", description = "用户更新密码")
    @ApiResponses(value = {
        @ApiResponse(responseCode = "200", description = "密码更新成功"),
        @ApiResponse(responseCode = "400", description = "旧密码错误")
    })
    public boolean updatePassword(@Parameter(description = "用户ID") @RequestParam Integer userId, 
                                @Parameter(description = "旧密码") @RequestParam String oldPassword, 
                                @Parameter(description = "新密码") @RequestParam String newPassword) { 
        return userService.updatePassword(userId, oldPassword, newPassword); 
    }
    
    @PostMapping("/token/refresh")
    @Operation(summary = "刷新令牌", description = "使用刷新令牌获取新的访问令牌")
    @ApiResponses(value = {
        @ApiResponse(responseCode = "200", description = "令牌刷新成功"),
        @ApiResponse(responseCode = "401", description = "刷新令牌无效")
    })
    public String refreshToken(@Parameter(description = "刷新令牌") @RequestParam String refreshToken) { 
        return userService.refreshToken(refreshToken); 
    }
} 